package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.LikeBehaviorService;
import com.heima.common.constants.BehaviorConstans;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.LikeBehaviorDto;
import com.heima.model.behavior.ReadBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.message.Message;
import com.heima.utils.thread.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LikeBehaviorServiceImpl implements LikeBehaviorService {
    @Autowired
    private CacheService cacheService;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 处理点赞和取消点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikeBehaviorDto dto) {
        if (ThreadLocalUtil.get() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer likeNum = 0;
        if (dto.getOperation() == BehaviorConstans.LIKE) {
            cacheService.hPut(ThreadLocalUtil.get() + "_" + dto.getArticleId() + BehaviorConstans.CACHE_END, "like", "0");
            likeNum = 1;
        } else if (dto.getOperation() == BehaviorConstans.CANCLE_LIKE) {
            cacheService.hDelete(ThreadLocalUtil.get() + "_" + dto.getArticleId() + BehaviorConstans.CACHE_END, "like");
            likeNum = -1;
        }
        Message message = new Message(dto.getArticleId());
        message.setLike(likeNum);
        kafkaTemplate.send(BehaviorConstans.HOT_ARTICLE_AGG_TOPIC, JSON.toJSONString(message));
        log.info("成功发送：{}", message);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 处理不喜欢和取消不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(LikeBehaviorDto dto) {
        if (ThreadLocalUtil.get() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getType() == BehaviorConstans.NOT_LIKE) {
            cacheService.hPut(ThreadLocalUtil.get() + "_" + dto.getArticleId() + BehaviorConstans.CACHE_END, "unlike", "0");
        } else if (dto.getType() == BehaviorConstans.CANCLE_NOT_LIKE) {
            cacheService.hDelete(ThreadLocalUtil.get() + "_" + dto.getArticleId() + BehaviorConstans.CACHE_END, "unlike");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 处理阅读量
     * @param dto
     * @return
     */
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Message message = new Message(dto.getArticleId());
        message.setView(1);
        kafkaTemplate.send(BehaviorConstans.HOT_ARTICLE_AGG_TOPIC, JSON.toJSONString(message));
        log.info("成功发送：{}", message);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
